package com.chennan.javabase.domain.struct;

import lombok.val;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author chennan
 * @createTime 2021年07月22日 10:17:00
 */
public class SwapLinked {


//    public void

    public static void main(String[] args) {




        LinkList.Node head = new LinkList.Node(1);
        LinkList.Node node1 = new LinkList.Node(2);
        LinkList.Node node2 = new LinkList.Node(3);
        LinkList.Node node3 = new LinkList.Node(4);
        LinkList.Node down = new LinkList.Node(5);
        head.prev=null;
        head.next=node1;
        node1.prev=head;
        node1.next = node2;
        node2.prev=node1;
        node2.next=node3;
        node3.prev=node2;
        node3.next=down;
        down.next=null;
        down.prev=node3;



        LinkList.Node headTEMP =head;

        while ((headTEMP = headTEMP.next)!=null) {
            System.out.println(headTEMP.item);
        }

        for (int i = 0; i < 5; i++) {
            System.out.println(headTEMP.item);
            headTEMP= headTEMP.next;
        }

        LinkList.swap(node2,node3);

        headTEMP =head;
        for (int i = 0; i < 5; i++) {
            System.out.println(headTEMP.item);
            headTEMP= headTEMP.next;
        }



//        aaa.swap();
    }


}



//  Definition for singly-linked list.
class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
}

class Solution {
    public int[] reversePrint(ListNode head) {

        if(head == null)return new int[0];
        List<Integer> list = new ArrayList<>();
        ListNode headTEMP = head;
        list.add(headTEMP.val);
        while ((headTEMP = headTEMP.next)!=null) {
            list.add(headTEMP.val);
        }
        int[] arr =new int[list.size()];
        for (int i = list.size()-1; i >0; i--) {
            arr[i]=list.get(i);
        }
        return arr;
    }
}

class LinkList<E> {


    public static void swap(Node a,Node b){
        //前节点为null，说明是头节点
        if (a==b) {
            return;
        }
        Node bnext = b.next;
        Node bprev = b.prev;
        Node anext = a.next;
        Node aprev = a.prev;
        anext.prev=b;
        aprev.next=b;
        bnext.prev=a;
        bprev.next=a;
        b.next=anext;
        b.prev=aprev;
        a.next=bnext;
        a.prev=bprev;
    }


    public static class Node<E> {
        E item;
        LinkList.Node<E> next;
        LinkList.Node<E> prev;

        Node(E element) {
            this.item=element;
        }
    }

}